//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Input;

namespace OpenLS.Spreadsheet.UI.Internals
{
    internal static class CellTextEditorMouse
    {
        internal static void RegisterClassHandlers(Type controlType)
        {
            EventManager.RegisterClassHandler(controlType, UIElement.MouseDownEvent,new MouseButtonEventHandler(onMouseDown));
            EventManager.RegisterClassHandler(controlType, UIElement.MouseUpEvent, new MouseButtonEventHandler(onMouseUp));
            EventManager.RegisterClassHandler(controlType, UIElement.MouseMoveEvent, new MouseEventHandler(onMouseMove));
        }

        private static void onMouseMove(object sender, MouseEventArgs e)
        {
            var cb = (CellTextBox)sender;
            if (!cb.IsMouseCaptured)
                return;
            EditedCell cell = cb.EditedCell;
            Point d = e.GetPosition(cb);
            cb.SelectAt(d);
            if (cell != null)
                cell.Mode = EditionMode.Edit;
            e.Handled = true;


        }
        private static void onMouseUp(object sender, MouseButtonEventArgs e)
        {
            var cb = (CellTextBox)sender;
            cb.ReleaseMouseCapture();
            EditedCell cell = cb.EditedCell;
            Point d = e.GetPosition(cb);
            cb.SelectAt(d);
            if (cell != null)
                cell.Mode = EditionMode.Edit;
            e.Handled = true;

            
        }
        private static void onMouseDown(object sender, MouseButtonEventArgs e)
        {
            var cb = (CellTextBox) sender;
            cb.CaptureMouse();
            cb.Focus();
            EditedCell cell = cb.EditedCell;
            Point d = e.GetPosition(cb);
            cb.MoveAt(d);
            if (cell != null)
                cell.Mode = EditionMode.Edit;
            e.Handled = true;
        }
    }
}